﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Windows.Forms;

namespace UnrealConverterLogic
{
    public partial class UnrealConverter
    {
        private static void ReadMainInputs(XmlDocument d)
        {
            CurrentConversion.SetName       = d.SelectSingleNode("/Root/Inputs").Attributes["set-name"].Value;
            CurrentConversion.UnrealUnits   = Double.Parse(d.SelectSingleNode("/Root/Inputs").Attributes["unreal-units"].Value);
            CurrentConversion.MaxUnits      = Double.Parse(d.SelectSingleNode("/Root/Inputs").Attributes["max-units"].Value);
            CurrentConversion.Meters        = Double.Parse(d.SelectSingleNode("/Root/Inputs").Attributes["meters"].Value);

            UsedRatio.SetName               = d.SelectSingleNode("/Root/Ratios").Attributes["set-name"].Value;
            UsedRatio.UnrealUnits           = Double.Parse(d.SelectSingleNode("/Root/Ratios").Attributes["unreal-units"].Value);
            UsedRatio.MaxUnits              = Double.Parse(d.SelectSingleNode("/Root/Ratios").Attributes["max-units"].Value);
            UsedRatio.Meters                = Double.Parse(d.SelectSingleNode("/Root/Ratios").Attributes["meters"].Value);
        }
        private static void ReadRatios(XmlDocument d)
        {

            XmlNodeList l = d.GetElementsByTagName("Ratio-Sets");
            foreach (XmlNode n in l)
            {
                foreach (XmlNode node in n.ChildNodes)
                {
                    string[] RatioSet = new string[4];
                    RatioSet[0] = node.Attributes["set-name"].Value.ToString();
                    RatioSet[1] = node.Attributes["unreal-units"].Value.ToString();
                    RatioSet[2] = node.Attributes["max-units"].Value.ToString();
                    RatioSet[3] = node.Attributes["meters"].Value.ToString();
                    Ratio r = new Ratio
                    (
                        RatioSet[0], 
                        Double.Parse(RatioSet[1]), 
                        Double.Parse(RatioSet[2]), 
                        Double.Parse(RatioSet[3])
                    );
                    AddRatio(r);
                }
            }
        }
        private static void ReadReferences(XmlDocument d)
        {
            references = new List<Reference>();

            XmlNodeList l = d.GetElementsByTagName("References");
            foreach (XmlLinkedNode n in l)
            {
                foreach (XmlNode node in n.ChildNodes)
                {
                    Reference r = new Reference
                    (
                        node.Attributes["set-name"].Value.ToString(),
                        Double.Parse(node.Attributes["unreal-units"].Value.ToString()),
                        Double.Parse(node.Attributes["max-units"].Value.ToString()),
                        Double.Parse(node.Attributes["meters"].Value.ToString()),
                        null
                    );
                    AddReference(r);
                }
            }
        }
        public static void ReadXML(object sender, EventArgs e)
        {
            XmlDocument documents = new XmlDocument();
            try
            {
                documents.Load("Project.xml");

                ReadMainInputs(documents);
                ReadReferences(documents);
                ReadRatios(documents);
            }
            catch (System.IO.FileNotFoundException notfound) { }
        }
        private static void WriteRatioRows(XmlWriter w)
        {
            w.WriteStartElement("Ratio-Sets");
            foreach (Ratio r in Ratios)
            {
                WriteInputs(w, "Ratio-Set", r.ToStringArray());
            }
            w.WriteEndElement();
        }

        private static void WriteReferenceRows(XmlWriter w)
        {
            w.WriteStartElement("References");
            foreach (Reference r in references)
            {
                WriteInputs(w, "Reference", r.ToArray());
            }
            w.WriteEndElement();
        }
        private static void WriteInputs(XmlWriter w, string setName, string[] inputs)
        {
            w.WriteStartElement(setName);
            {
                w.WriteAttributeString("set-name", inputs[0]);

                w.WriteAttributeString("unreal-units", inputs[1]);

                w.WriteAttributeString("max-units", inputs[2]);

                w.WriteAttributeString("meters", inputs[3]);
            }
            w.WriteEndElement();
        }
        private static void WriteCurrentConversion(XmlWriter w)
        {
            WriteInputs(w, "Inputs", CurrentConversion.ToStringArray());
        }
        private static void WriteUsedRatio(XmlWriter w)
        {
            string[] ratioset = new string[]
            {
                UsedRatio.SetName,
                UsedRatio.UnrealUnits.ToString(),
                UsedRatio.MaxUnits.ToString(),
                UsedRatio.Meters.ToString()
            };

            WriteInputs(w, "Ratios", ratioset);

        }
        public static void WriteOutXML(object sender, EventArgs e)
        {
            XmlWriterSettings xws = new XmlWriterSettings();
            xws.Indent = true;
            xws.IndentChars = "\t";

            using (XmlWriter writer = XmlWriter.Create("Project.xml", xws))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("Root");
                {
                    WriteCurrentConversion(writer);

                    WriteUsedRatio(writer);

                    WriteReferenceRows(writer);
                    WriteRatioRows(writer);
                }
                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
        }
    }
}
